<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PostController
 *
 * @author tuancasi
 */
class PostController extends Controller {

    /**
     * show list  
     */
    public function actionIndex() {

	$model = new Posts('search');
	$model->unsetAttributes();  // clear any default values

	if (isset($_GET['Posts'])) {
	    $model->attributes = $_GET['Posts'];
	}
	/**
	 * get all category
	 */
	$aCats = Yii::app()->db->createCommand('SELECT category_id, name FROM {{categories}} WHERE parent_id<>-1 ')->queryAll();

	$aCategories = array();
	foreach ($aCats as $cat) {
	    $aCategories[$cat['category_id']] = $cat['name'];
	}

	$this->render('index', array(
	    'model' => $model,
	    'aCategories' => $aCategories
	));
    }

    /*     * ***********************************************************
     * add post
     */

    public function actionCreate($icat = 1) {

	$modelPost = new Posts();
	$modelCat = new Category();
	$success = 0; //alert that posting is successful
	$error = ''; //alert that posting is fail
	$modelPost->is_private = 1;
	$oCats = $modelCat->getSubCat($icat);
	//if user press post
	if (isset($_POST['submit'])) {

	    if ($icat == 3) {
		$modelPost->scenario = 'postEvent';
	    }
	    $modelPost->attributes = $_POST['Posts'];
	    if ($modelPost->validate()) {

		//if user upload file
		if (isset($_FILES['photo']['name']) || $_POST['sharelink']) {

		    $urlPhoto = '';
		    if (isset($_FILES['photo']['name'])) {
			$ext = strtolower(pathinfo($_FILES["photo"]["name"], PATHINFO_EXTENSION));
			$newName = uniqid() . '.' . $ext;
			if (move_uploaded_file($_FILES['photo']['tmp_name'], UPLOAD_PATH_POST . $newName)) {
			    $urlPhoto = $newName;
			}
		    }

		    $aAttachment = array('photo' => $urlPhoto, $_POST['typeEmbedded'] => $_POST['sharelink']);
		    $modelPost->attachment = serialize($aAttachment);
		}
		//if post event
		if ($icat == 3) {
		    if (isset($_POST['date_start_event']) && $_POST['date_start_event'] != '' &&
			    isset($_POST['time_start_event']) && $_POST['time_start_event'] != '' &&
			    isset($_POST['date_end_event']) && $_POST['date_end_event'] != '' &&
			    isset($_POST['time_end_event']) && $_POST['time_end_event'] != '') {
			$startDateEvent = $_POST['date_start_event'] . ' ' . $_POST['time_start_event'];
			$endDateEvent = $_POST['date_end_event'] . ' ' . $_POST['time_end_event'];
			if (strtotime($startDateEvent) >= strtotime($endDateEvent)) {
			    $error = 'Start time should be less than end time.';
			} else {
			    $modelPost->time_start_event = $startDateEvent;
			    $modelPost->time_end_event = $endDateEvent;
			}
		    } else {
			$error = 'Date & Time cannot be blank';
		    }
		}
		$modelPost->user_id = Yii::app()->user->id;
		$modelPost->time_created = date('Y-m-d H:i:s', CURR_TIME);
		if ($error == '') {
		    if ($modelPost->save()) {
			$success = 1;
		    }
		}
	    }
	}
	//change array for select box
	$aData = array();
	foreach ($oCats as $item) {
	    $aData[$item->category_id] = $item->name;
	}

	//get all parent categories
	$aParentCats = Yii::app()->db->createCommand("SELECT category_id, name FROM {{categories}} WHERE parent_id=-1")->queryAll();

	$this->render('create', array(
	    'icat' => $icat,
	    'modelPost' => $modelPost,
	    'aData' => $aData,
	    'aParentCats' => $aParentCats,
	    'success' => $success,
	    'error' => $error
	));
    }

    /*     * ***********************************************************
     * edit post
     */

    public function actionUpdate() {
	if (isset($_GET['id'])) {
	    $success = 0; //alert that posting is successful
	    $error = ''; //alert that posting is fail	
	    $modelCat = new Category();
	    $modelPost = Posts::model()->findByPk($_GET['id']);
	    $icat = Yii::app()->db->createCommand("SELECT parent_id FROM {{categories}} WHERE category_id=" . $modelPost->category_id)->queryRow();
	    $icat = $icat['parent_id'];
	    $aAttachment = unserialize($modelPost->attachment);
	    //if user update
	    if (isset($_POST['submit'])) {
		if ($icat == 3) {
		    $modelPost->scenario = 'postEvent';
		}
		$modelPost->attributes = $_POST['Posts'];
		if ($modelPost->validate()) {
		    //if user upload file
		    if (isset($_FILES['photo']['name']) || $_POST['sharelink']) {

			$urlPhoto = $aAttachment['photo'];
			if (isset($_FILES['photo']['name']) && $_FILES['photo']['name']) {


			    //delete old file
			    if ($aAttachment['photo'] && file_exists(UPLOAD_PATH_POST . $aAttachment['photo'])) {
				unlink(UPLOAD_PATH_POST . $aAttachment['photo']);
			    }

			    $ext = strtolower(pathinfo($_FILES["photo"]["name"], PATHINFO_EXTENSION));
			    $newName = uniqid() . '.' . $ext;
			    if (move_uploaded_file($_FILES['photo']['tmp_name'], UPLOAD_PATH_POST . $newName)) {
				$urlPhoto = $newName;
			    }
			}
			$aAttachment = array('photo' => $urlPhoto, $_POST['typeEmbedded'] => $_POST['sharelink']);
			$modelPost->attachment = serialize($aAttachment);
		    }
		    //if post event
		    if ($icat == 3) {
			if (isset($_POST['date_start_event']) && $_POST['date_start_event'] != '' &&
				isset($_POST['time_start_event']) && $_POST['time_start_event'] != '' &&
				isset($_POST['date_end_event']) && $_POST['date_end_event'] != '' &&
				isset($_POST['time_end_event']) && $_POST['time_end_event'] != '') {
			    $startDateEvent = $_POST['date_start_event'] . ' ' . $_POST['time_start_event'];
			    $endDateEvent = $_POST['date_end_event'] . ' ' . $_POST['time_end_event'];
			    if (strtotime($startDateEvent) > strtotime($endDateEvent)) {
				$error = 'Start time should be less than end time.';
			    } else {
				$modelPost->time_start_event = $startDateEvent;
				$modelPost->time_end_event = $endDateEvent;
			    }
			} else {
			    $error = 'Date & Time cannot be blank';
			}
		    }
		    $modelPost->time_created = date('Y-m-d H:i:s', CURR_TIME);
		    if ($error == '') {
			if ($modelPost->save()) {
			    $success = 1;
			}
		    }
		}
	    }
	    $oCats = $modelCat->getSubCat($icat);
	    //change array for select box
	    $aData = array();
	    foreach ($oCats as $item) {
		$aData[$item->category_id] = $item->name;
	    }
	    $this->render('update', array(
		'icat' => $icat,
		'modelPost' => $modelPost,
		'aData' => $aData,
		'success' => $success,
		'error' => $error,
		'aAttachment' => $aAttachment
	    ));
	}
    }

    /*     * ************************************************************************************
     * delete post
     */

    public function actionDelete() {
	if (isset($_GET['id'])) {
	    $modelPost = Posts::model()->findByPk(intval($_GET['id']))->delete();
	}
    }

    /*     * ******************************************************************************************
     * view post
     */

    public function actionView() {
	if (isset($_GET['id'])) {
	    $model = Posts::model()->findByPk(intval($_GET['id']));
	    $parentCat = Yii::app()->db->createCommand('SELECT parent_id FROM {{categories}} WHERE category_id=' . $model->category_id)->queryRow();

	    $this->render('view', array(
		'model' => $model,
		'parentCat' => $parentCat['parent_id']
	    ));
	}
    }

    /*     * *********************************REPORT ABUSE ***************************** */
    /*
     * view report abuse
     */

    public function actionReportAbuse() {
	$model = new Abuse('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['Abuse'])) {
	    $model->attributes = $_GET['Abuse'];
	}
	/**
	 * get all category
	 */
	$aStAbuse = Yii::app()->db->createCommand('SELECT * FROM {{statement_abuses}}')->queryAll();
	$aStatementAbuse = array();
	foreach ($aStAbuse as $abuse) {
	    $aStatementAbuse[$abuse['text']] = $abuse['text'];
	}

	$this->render('reportabuse', array(
	    'model' => $model,
	    'aStatementAbuse' => $aStatementAbuse
	));
    }

    /*
     * delete  report
     */

    public function actionDeleteReportAbuse() {
	if (isset($_GET['id'])) {
	    $model = Abuse::model()->findByPk(intval($_GET['id']))->delete();
	}
    }

}

